/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgAddReport.java
 *
 * Created on Aug 18, 2012, 4:09:54 PM
 */

package Model.dialog;

import DataBase.DBManagerment;
import Processing.dbclass.Report;
import java.sql.Connection;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class dlgEditReport extends javax.swing.JDialog {

    /** Creates new form dlgAddReport */
    public dlgEditReport(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * Đối tượng của class DBManagerment
     */
    private DBManagerment db = new DBManagerment();

    /**
     * Một Connection
     */
    private Connection con = db.ConnectServer();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlReportAdd = new javax.swing.JPanel();
        jLabel30 = new javax.swing.JLabel();
        jLabel32 = new javax.swing.JLabel();
        jScrollPane14 = new javax.swing.JScrollPane();
        taReportAddPatientReport = new javax.swing.JTextArea();
        btnReportAction = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        txtReportAddDoctorID = new javax.swing.JTextField();
        jLabel36 = new javax.swing.JLabel();
        jScrollPane15 = new javax.swing.JScrollPane();
        taReportAddDoctorReport = new javax.swing.JTextArea();
        jLabel33 = new javax.swing.JLabel();
        txtReportTestPurchase = new javax.swing.JTextField();
        jLabel37 = new javax.swing.JLabel();
        txtReportAddTest = new javax.swing.JTextField();
        txtReportAddPatientID = new javax.swing.JTextField();
        jLabel38 = new javax.swing.JLabel();
        jLabel31 = new javax.swing.JLabel();
        txtReportID = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edit Report");
        setResizable(false);

        pnlReportAdd.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
        pnlReportAdd.setPreferredSize(new java.awt.Dimension(390, 545));

        jLabel30.setText("Test Purchase's ID:");

        jLabel32.setText("Patient's Report:");

        taReportAddPatientReport.setColumns(20);
        taReportAddPatientReport.setRows(5);
        jScrollPane14.setViewportView(taReportAddPatientReport);

        btnReportAction.setText("Edit");
        btnReportAction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReportActionActionPerformed(evt);
            }
        });

        jButton5.setText("Cancel");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        txtReportAddDoctorID.setEnabled(false);

        jLabel36.setText("Doctor's ID:");

        taReportAddDoctorReport.setColumns(20);
        taReportAddDoctorReport.setRows(5);
        jScrollPane15.setViewportView(taReportAddDoctorReport);

        jLabel33.setText("Doctor's Report:");

        txtReportTestPurchase.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtReportTestPurchaseFocusLost(evt);
            }
        });

        jLabel37.setText("Test:");

        txtReportAddTest.setFont(new java.awt.Font("Tahoma", 1, 12));
        txtReportAddTest.setEnabled(false);

        txtReportAddPatientID.setFont(new java.awt.Font("Tahoma", 1, 12));
        txtReportAddPatientID.setEnabled(false);

        jLabel38.setText("Patient's ID:");

        jLabel31.setText("Report's ID:");

        txtReportID.setFont(new java.awt.Font("Tahoma", 1, 12));
        txtReportID.setEnabled(false);
        txtReportID.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtReportIDFocusLost(evt);
            }
        });

        javax.swing.GroupLayout pnlReportAddLayout = new javax.swing.GroupLayout(pnlReportAdd);
        pnlReportAdd.setLayout(pnlReportAddLayout);
        pnlReportAddLayout.setHorizontalGroup(
            pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlReportAddLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlReportAddLayout.createSequentialGroup()
                        .addGroup(pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlReportAddLayout.createSequentialGroup()
                                .addGroup(pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel30)
                                    .addComponent(jLabel38)
                                    .addComponent(jLabel36)
                                    .addComponent(jLabel32)
                                    .addComponent(jLabel33)
                                    .addComponent(jLabel37))
                                .addGap(27, 27, 27)
                                .addGroup(pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane15, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                                    .addComponent(txtReportAddDoctorID, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtReportAddPatientID, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtReportTestPurchase, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtReportAddTest, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane14, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtReportID, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(pnlReportAddLayout.createSequentialGroup()
                                .addGap(110, 110, 110)
                                .addComponent(btnReportAction, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(57, 57, 57))
                    .addComponent(jLabel31)))
        );
        pnlReportAddLayout.setVerticalGroup(
            pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlReportAddLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel31)
                    .addComponent(txtReportID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel30)
                    .addComponent(txtReportTestPurchase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtReportAddTest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel37))
                .addGap(18, 18, 18)
                .addGroup(pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel38)
                    .addComponent(txtReportAddPatientID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel36)
                    .addComponent(txtReportAddDoctorID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel32)
                    .addComponent(jScrollPane14, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel33)
                    .addComponent(jScrollPane15, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlReportAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnReportAction)
                    .addComponent(jButton5))
                .addContainerGap())
        );

        getContentPane().add(pnlReportAdd, java.awt.BorderLayout.LINE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnReportActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReportActionActionPerformed
        // TODO add your handling code here:
        if (txtReportTestPurchase.getText().length() != 0) {
            try {
                int id = Integer.parseInt(txtReportTestPurchase.getText());

                if (id == 0) {
                    return;

                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "Test Purchase's ID input wrong!");
                return;
            }
        }

        if (txtReportTestPurchase.getText().length() == 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Test Purchase's ID must have!");
        }else if(db.searchTestPurchaseByID(con, Integer.parseInt(txtReportTestPurchase.getText())).getRowCount() == 0){
            // Kiểm tra xem có Patient này ko, dùng method search Patient theo PatientID
            // trả về PikaTableModel, rồi xem PikatableModel này có số row == 0 hay ko?
            // Nếu số Row == 0 tức là ko có Patient với PatientID này

            JOptionPane.showMessageDialog(this, "This Test Purchase's ID haven't existed!");
        }else if (txtReportAddDoctorID.getText().length() != 6) {
            //Kiển tra nếu nhập vào ko đúng 6 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Doctor's ID must have exactly six character!");
        }else if(db.searchStaff(con, 1, txtReportAddDoctorID.getText()).getRowCount() == 0){
            // Kiểm tra xem có Patient này ko, dùng method search Patient theo PatientID
            // trả về PikaTableModel, rồi xem PikatableModel này có số row == 0 hay ko?
            // Nếu số Row == 0 tức là ko có Patient với PatientID này

            JOptionPane.showMessageDialog(this, "This Doctor's ID haven't existed!");
        }else if (taReportAddPatientReport.getText().length() == 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Patient's report must have!");
        }else if (taReportAddDoctorReport.getText().length() == 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Doctor's report must have!");
        }else{
            // Trong add bill này CollectorID có thể để trống nên ko cần kiểm tra

            // Nếu toàn bộ quá trình kiểm tra ko sai thì bắt đầu add vào

            rep = new Report();

            int id = Integer.parseInt(txtReportTestPurchase.getText());

            // Tạo mới một Object Bill để chứa thông tin
            rep.setTestPurchaseID(id);

            String billId = db.searchTestPurchaseByID(con, id).getValueAt(0, 1).toString();            

            String patientID = db.searchBilling(con, 1, billId).getValueAt(0, 1).toString();

            rep.setPatientID(patientID);

            rep.setStaffID(txtReportAddDoctorID.getText());

            rep.setPatientReport(taReportAddPatientReport.getText());

            rep.setDoctorReport(taReportAddDoctorReport.getText());

            this.setVisible(false);

            // DateMade và DatePaid ko phải làm vì tớ đã sửa lại method add trong
            // DBManager, sẽ lấy ngày hiện tại
            // Report có DateMade tương tự cũng được tớ sủa method rồi
        }
    }//GEN-LAST:event_btnReportActionActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void txtReportTestPurchaseFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtReportTestPurchaseFocusLost
        // TODO add your handling code here:
        if (db.searchTestPurchaseByID(con, Integer.parseInt(txtReportTestPurchase.getText())).getRowCount() != 0) {
            String testName = db.searchTestPurchaseByID(con, Integer.parseInt(txtReportTestPurchase.getText())).getValueAt(0, 3).toString();

            String billId = db.searchTestPurchaseByID(con, Integer.parseInt(txtReportTestPurchase.getText())).getValueAt(0, 1).toString();

            String patientID = db.searchBilling(con, 1, billId).getValueAt(0, 1).toString();
            
            txtReportAddTest.setText(testName);
            txtReportAddPatientID.setText(patientID);
        }else{
            txtReportAddTest.setText("");
            txtReportAddPatientID.setText("");
        }
    }//GEN-LAST:event_txtReportTestPurchaseFocusLost

    private void txtReportIDFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtReportIDFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_txtReportIDFocusLost

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgEditReport dialog = new dlgEditReport(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    /**
     * Biến chứa dữ liệu của bill
     */
    private Report rep;

    /**
     * Method trả về bill để chương trình xử lý
     * @return Bill
     */
    public Report getReport() {
        return rep;
    }

    public void setReport(Report rep) {
        this.rep = rep;

        txtReportID.setText(""+rep.getReportID());

        txtReportTestPurchase.setText(db.searchReport(con, 1, ""+rep.getReportID()).getValueAt(0, 1).toString());

        txtReportAddTest.setText(db.searchReport(con, 1, ""+rep.getReportID()).getValueAt(0, 3).toString());

        txtReportAddPatientID.setText(db.searchReport(con, 1, ""+rep.getReportID()).getValueAt(0, 4).toString());

        txtReportAddDoctorID.setText(db.searchReport(con, 1, ""+rep.getReportID()).getValueAt(0, 6).toString());

        taReportAddPatientReport.setText(db.searchReport(con, 1, ""+rep.getReportID()).getValueAt(0, 9).toString());

        taReportAddDoctorReport.setText(db.searchReport(con, 1, ""+rep.getReportID()).getValueAt(0, 10).toString());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReportAction;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JLabel jLabel32;
    private javax.swing.JLabel jLabel33;
    private javax.swing.JLabel jLabel36;
    private javax.swing.JLabel jLabel37;
    private javax.swing.JLabel jLabel38;
    private javax.swing.JScrollPane jScrollPane14;
    private javax.swing.JScrollPane jScrollPane15;
    private javax.swing.JPanel pnlReportAdd;
    private javax.swing.JTextArea taReportAddDoctorReport;
    private javax.swing.JTextArea taReportAddPatientReport;
    private javax.swing.JTextField txtReportAddDoctorID;
    private javax.swing.JTextField txtReportAddPatientID;
    private javax.swing.JTextField txtReportAddTest;
    private javax.swing.JTextField txtReportID;
    private javax.swing.JTextField txtReportTestPurchase;
    // End of variables declaration//GEN-END:variables

}
